﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using INSTA.MusiqueStore.DAL;

namespace INSTA.MusiqueStore.UI.Web
{
    public partial class DownloadPiste : System.Web.UI.Page
    {
        protected void Page_PreRender(object sender, EventArgs e)
        {
            lstGenre.DataSource = DAL.CustomMusiqueStoreEntities.GetGenres();
            lstGenre.DataTextField = "Nom";
            lstGenre.DataValueField = "Id";
            lstGenre.DataBind();
            lstArtiste.DataSource = DAL.CustomMusiqueStoreEntities.GetArtists();
            lstArtiste.DataValueField = "Id";
            lstArtiste.DataTextField = "NomComplet";
            lstArtiste.DataBind();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            var d = new DirectoryInfo(Server.MapPath(DAL.CustomMusiqueStoreEntities.GetAlbumMusicPath(int.Parse(Request.Params["i"]))));//Assuming Test is your Folder
            Label1.Text = d.FullName;
            var files = d.GetFiles("*.mp3"); //Getting Text files
            foreach (var file in files)
            {
                var infos = Library.Tips.ReadInfos(file.FullName);
                var p = new Panel();
                bla.Controls.Add(p);
                p.Controls.Add(new TextBox() { Text = d.Name + "/" + file.Name });
                p.Controls.Add(new TextBox() { Text = infos.Tag.Title });
                p.Controls.Add(new TextBox() { Text = infos.Tag.Track.ToString() });
                p.Controls.Add(new TextBox() { Text = infos.Properties.Duration.TotalSeconds.ToString() });
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (Panel panel in bla.Controls)
            {
                var idAlbum = int.Parse(Request.Params["i"]);
                var idArtiste = int.Parse(lstArtiste.SelectedItem.Value);
                var dateSortie = Convert.ToDateTime(txtDate.Text);
                var testDuree = ((TextBox)panel.Controls[3]).Text;
                var duree = (int)double.Parse(((TextBox)panel.Controls[3]).Text);
                var idGenre = int.Parse(lstGenre.SelectedItem.Value);
                var lienFichier = ((TextBox)panel.Controls[0]).Text;
                var numero = int.Parse(((TextBox)panel.Controls[2]).Text);
                decimal prix = 0;
                prix = Convert.ToDecimal(double.Parse(txtPrix.Text));
                var titre = ((TextBox)panel.Controls[1]).Text;

                DAL.CustomMusiqueStoreEntities.AddTrack(idAlbum, idArtiste, dateSortie, duree, idGenre, lienFichier,
                                                        numero, prix, titre);

            }
            Response.Redirect("~/Details.aspx?i=" + Request["i"]);
        }


    }
}